package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.query.EmployeeQueryObject;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    public IEmployeeService employeeService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"employee:view", "员工列表"}, logical = Logical.OR)
    public String view() {
        return "employee/employee";
    }

    @ResponseBody
    @RequestMapping("query")
    public PageResult query(EmployeeQueryObject qo) {
        PageResult query = employeeService.query(qo);
        return query;
    }

    @ResponseBody
    @RequestMapping("getSaleMen")
    public List<Employee> getSaleMen() {
        return employeeService.getSaleMen();
    }



    @ResponseBody
    @RequestMapping("selectByPrimaryKey")
    public Employee selectByPrimaryKey(Long id) {
        Employee emp = employeeService.selectByPrimaryKey(id);
        return emp ;
    }

    @ResponseBody
    @RequestMapping("saveOrUpdate")
    @RequiresPermissions(value = {"employee:saveOrUpdate", "员工增加或编辑"}, logical = Logical.OR)
    public JSONResult saveOrUpdate(Employee employee) {
        try {
            if (employee.getId() != null) {
                employeeService.updateByPrimaryKey(employee);
            } else {
                employeeService.insert(employee);
            }
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("操作失败");
        }
    }

    @RequestMapping("validateUsername")
    @ResponseBody
    public boolean validateUsername(String username, Long id) {
        Employee employee = employeeService.getByUsername(username);
        return employee == null || employee.getId() == id;
    }

    @RequestMapping("changeState")
    @ResponseBody
    @RequiresPermissions(value = {"employee:changeState", "员工离职或复职"}, logical = Logical.OR)
    public JSONResult changeState(Long id) {
        try {
            employeeService.changState(id);
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("操作失败");
        }
    }

    @RequestMapping("resetPassword")
    @ResponseBody
    @RequiresPermissions(value = {"employee:resetPassword", "员工重置密码"}, logical = Logical.OR)
    public JSONResult resetPassword(String newPassword, Long id) {
        try {
            employeeService.resetPassword(newPassword, id);
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("操作失败");
        }
    }

    @RequestMapping("getRoleByEmployeeId")
    @ResponseBody
    public List<Long> getRoleByEmployeeId(Long employeeId) {
        return employeeService.getRoleByEmployeeId(employeeId);
    }

    @RequestMapping("getByTeacherRoleSn")
    @ResponseBody
    public List<Employee> getByTeacherRoleSn(String sn) {
        List<Employee> list = employeeService.getByTeacherRoleSn(sn);
        return list;
    }

    @RequestMapping("exportXls")
    public void exportXls(EmployeeQueryObject qo, HttpServletResponse response) throws IOException {
        response.setHeader("Content-disposition", "attachment;filename=employee.xls");
        HSSFWorkbook workbook = employeeService.exportXls(qo);
        workbook.write(response.getOutputStream());
    }

    @RequestMapping("importXls")
    public JSONResult importXls(MultipartFile file){
        try {
            employeeService.importXls(file);
            return JSONResult.success("导入成功");
        } catch (IOException e) {
            e.printStackTrace();
            return JSONResult.mark("导入失败");
        }
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<Employee> selectAll() {
        return employeeService.selectAll();
    }

}
